//
//  HYQPhotoBrowser.m
//  HYQPhotoUploader
//
//  Created by __无邪_ on 2018/9/5.
//  Copyright © 2018 __无邪_. All rights reserved.
//

#import "HYQPhotoBrowser.h"


@interface HYQPhotoBrowser ()<MWPhotoBrowserDelegate>

@property (nonatomic, strong) NSMutableArray *photos;
@property (nonatomic, strong) MWPhotoBrowser *browser;
@property (nonatomic, weak) UIViewController *fromVC;

@end

@implementation HYQPhotoBrowser

- (instancetype)init
{
    self = [super init];
    if (self) {
        _browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
        _browser.enableSwipeToDismiss = YES;
    }
    return self;
}

- (void)showPhotos:(NSArray<MWPhoto *> *)photos
             index:(NSInteger)index
  animatedFromView:(UIView *)view
            inCtrl:(UIViewController *)controller;
{
    if (!photos.count) {
        return;
    }
    self.fromVC = controller;
    self.photos = [NSMutableArray arrayWithArray:photos];
    [self.browser setCurrentPhotoIndex:index];
    
    UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:self.browser];
    navC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [controller presentViewController:navC animated:YES completion:nil];
    
    [self.browser showNextPhotoAnimated:YES];
    [self.browser showPreviousPhotoAnimated:YES];
    [self.browser setCurrentPhotoIndex:index];
}


#pragma mark - MWPhotoBrowserDelegate

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.photos.count;
}
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < self.photos.count) {
        return [self.photos objectAtIndex:index];
    }
    return nil;
}
- (void)photoBrowserDidFinishModalPresentation:(MWPhotoBrowser *)photoBrowser {
    [photoBrowser dismissViewControllerAnimated:YES completion:nil];
}

- (NSString *)photoBrowser:(MWPhotoBrowser *)photoBrowser titleForPhotoAtIndex:(NSUInteger)index;
{
    return [NSString stringWithFormat:@"%zd / %zd", (index + 1), self.photos.count];
}

@end
